//
//  submitVC.m
//  dianfan
//
//  Created by Ye Jin on 15/11/9.
//  Copyright © 2015年 Ye Jin. All rights reserved.
//

#import "submitVC.h"
#import <BmobSDK/bmob.h>
#import "logoutVC.h"
#import "MBProgressHUD.h"

@interface submitVC ()

@property (nonatomic) BmobUser *bUser;
@property (weak, nonatomic) IBOutlet UILabel *dishLabel;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;

@end

@implementation submitVC
- (IBAction)plusBtn:(id)sender {
    int num = [self.numLabel.text intValue];
    num++;
    self.numLabel.text = [NSString stringWithFormat:@"%d",num];
}

- (IBAction)minusBtn:(id)sender {
    int num = [self.numLabel.text intValue];
    if (num != 1) {
        num--;
        self.numLabel.text = [NSString stringWithFormat:@"%d",num];
    }
}

- (void)logout
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    logoutVC *lvc = [sb instantiateViewControllerWithIdentifier:@"logout"];
    [self.navigationController pushViewController:lvc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dishLabel.text = [NSString stringWithFormat:@"%@: %@",self.restaurant.name,self.dish.name];
    self.bUser = [BmobUser getCurrentUser];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:self.bUser.username
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(logout)];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submit:(id)sender {
    //转菊花
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    hud.labelText=@"loading";

    BmobObject  *order = [BmobObject objectWithClassName:@"Order"];
    BmobObject *restaurant = [BmobObject objectWithoutDatatWithClassName:@"Restaurant" objectId:self.restaurant.ID];
    BmobObject *dish = [BmobObject objectWithoutDatatWithClassName:@"Menu" objectId:self.dish.ID];
    [order setObject:self.bUser forKey:@"user"];
    [order setObject:restaurant forKey:@"restaurant"];
    [order setObject:dish forKey:@"dish"];
    [order setObject:[NSNumber numberWithInt:[self.numLabel.text intValue]] forKey:@"dishNum"];
    //异步保存
    [order saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提交成功" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        } else if (error){
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@",error] message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];

        } else {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unknow error" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];

        }
        
    }];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
